/*******************************************************************************
 * Copyright © 2012 Ben Swim
 *
 * This file is part of ImoSPC.
 *
 * ImoSPC is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * ImoSPC is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * ImoSPC. If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package net.smwcentral;

import java.util.Map;

/**
 * Describes an SPC file.
 * 
 * @author ShadowFan-X
 */
public class SpcDesc {
	/** The title of the SPC file. */
	public final String title;
	/** The file name of an SPC file. */
	public final String file;
	/** The full path of an SPC file. */
	public final String path;
	/** The length of an SPC file, in seconds. */
	public final double length;
	
	SpcDesc(byte[] spc, String path) {
		this.title = DataHelper.getSpcTitle(spc);
		
		this.path = path.replace('\\', '/');
		this.file = path.substring(Math.max(0, path.lastIndexOf('/')));
		
		RefInt s = new RefInt(), l = new RefInt();
		DataHelper.getSpcFileLength(spc, s, l);
		
		this.length = (double)(s.value&0xFFFFFFFFL) +
						((double)(l.value&0xFFFFFFFFL) / 1000.0);
	}
	
	SpcDesc(Map.Entry<String, byte[]> entry) {
		this(entry.getValue(), entry.getKey());
	}

	@Override public String toString() {
		long min = (int)(length / 60);
		int sec = (int)Math.round(length - (min * 60.0));
		// Make sure sec < 60
		if (sec >= 60) {
			min++;
			sec -= 60;
		}
		return path + ": " + title + " (" + min + ':' +
					String.format("%02d", (int)sec) + ')';
	}
}
